//
//  DinnerDataHandel.m
//  Simple Life
//
//  Created by lanou3g on 15/11/12.
//  Copyright © 2015年 wyt. All rights reserved.
//

#import "DinnerDataHandel.h"

@interface DinnerDataHandel ()



@end


@implementation DinnerDataHandel

+(DinnerDataHandel *)defauDinnerDataHandel
{
    static dispatch_once_t onceToken;
    static DinnerDataHandel *dinnerDataHandel = nil;
    
    dispatch_once(&onceToken, ^{
        dinnerDataHandel = [DinnerDataHandel new];
        dinnerDataHandel.dataArray = [NSMutableArray array];
        [dinnerDataHandel loadDinnerData];
    });
    return dinnerDataHandel;
}
-(void)loadDinnerData
{
 
       
        NSString *string = @"http://wenyijcc.com/services/wenyiapp/recipehandler.ashx?action=list&op=0&catalog=0&timeline=2015-10-27T20:41:36.59&pagesize=10&EcodeStr=40369FBCE8A92D33DFC810CB3A88E515";
        
        NSURL *url = [NSURL URLWithString:string];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        NSURLSession *session = [NSURLSession sharedSession];
    
        NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
           
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingAllowFragments) error:nil];
            NSArray *sourcearray = dic[@"datas"];
            NSLog(@"%@",sourcearray);
            for (NSDictionary *dict in sourcearray) {
                
                
                DinnerModel *m = [[DinnerModel alloc] init];
                [m setValuesForKeysWithDictionary:dict];
                NSLog(@"%@",m.RecipeName);
                [self.dataArray addObject:m];
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [[NSNotificationCenter defaultCenter] postNotificationName:@"finishData" object:nil];
            });
            
            
        }];
        
        [task resume];
        
    
    
    
    
    
    
}
-(NSInteger )modelCount
{
    return self.dataArray.count;
}

-(DinnerModel *)modelWithIndex:(NSInteger)index
{
    return self.dataArray[index];
}






@end
